home *** CD-ROM | disk | FTP | other *** search
/ Programmer Plus 2007 / Programmer-Plus-2007.iso / Programming / Report Writers / Crystal Repot 9.0 Full CD version / Setup.exe / SRC / HOARDDLL.ZIP / 3rdParty / hoard / libhoard-2.0.2 / privateheap.h < prev    next >
Encoding:
C/C++ Source or Header  |  2000-03-15  |  2.0 KB  |  96 lines

  1. // -*- C++ -*-
  2.  
  3. #ifndef _PRIVATEHEAP_H_
  4. #define _PRIVATEHEAP_H_
  5.  
  6. #include <string.h>
  7.  
  8. #include "config.h"
  9. #include "arch-specific.h"
  10. #include "heap.h"
  11.  
  12. class processHeap;
  13.  
  14. class privateHeap : public hoardHeap {
  15.  
  16. public:
  17.  
  18.   privateHeap (void);
  19.  
  20.   // Set our process heap.
  21.   inline void setpHeap (processHeap * p);
  22.  
  23.   // Memory allocation and deallocation routines.
  24.   void * malloc (const size_t sz);
  25.   inline void * memalign (size_t alignment, size_t sz);
  26.   void free (void * ptr);
  27.  
  28.   // Find out how large an allocated object is.
  29.   inline static size_t objectSize (void * ptr);
  30.  
  31. private:
  32.  
  33.   enum { HEAP_REFILL_SIZE = 1048576 }; // 1 MB
  34.  
  35.   // Prevent copying and assignment.
  36.   privateHeap (const privateHeap&);
  37.   const privateHeap& operator= (const privateHeap&);
  38.  
  39.   void * _arena;     // Current arena of available memory.
  40.   int _arenaRemaining;
  41.   int _inUse;
  42.   int _maxInUse;
  43.  
  44.   block * _blocks[hoardHeap::SIZE_CLASSES];
  45.   processHeap *    pHeap;    // Our process heap.
  46.  
  47.   double _pad[CACHE_LINE / sizeof(double)];    // Cache pad.
  48. };
  49.  
  50.  
  51. void * privateHeap::memalign (size_t alignment,
  52.                  size_t size)
  53. {
  54.   // Calculate the amount of space we need
  55.   // to satisfy the alignment requirements.
  56.  
  57.   while (alignment != hoardHeap::align (alignment))
  58.     alignment <<= 1;
  59.  
  60.   size = size + alignment - 1;
  61.  
  62.   // Now malloc the space up with a little extra
  63.   // (we'll put the block pointer in right behind
  64.   // the allocated space).
  65.  
  66.   void * ptr = malloc (size + sizeof(block));
  67.  
  68.   void * newptr = (void *)
  69.     ((((unsigned int) ptr + alignment - 1) / alignment) * alignment);
  70.  
  71.   // Copy the block from the start of the allocated memory.
  72.  
  73.   block * b = ((block *) ptr - 1);
  74.   block * p = ((block *) newptr - 1);
  75.   memcpy (p, b, sizeof(block));
  76.  
  77.   return newptr;
  78. }
  79.  
  80.  
  81. size_t privateHeap::objectSize (void * ptr) 
  82. {
  83.   block * b = ((block *) ptr - 1);
  84.   assert (b->isValid());
  85.   return b->getActualSize();
  86. }
  87.  
  88.  
  89. void privateHeap::setpHeap (processHeap * p) 
  90. {
  91.   pHeap = p; 
  92. }
  93.  
  94. #endif // _PRIVATEHEAP_H_
  95.  
  96.